<?php

class ErrorController extends Engine_Controller_Action {
	
	protected $log;
	protected $translate;
	
	public function init() {
		$this->log = Zend_Registry::get('Zend_Log');
		$this->translate = Zend_Registry::get('Zend_Translate');
	}
	
	public function errorAction() {

		$errors = $this->_getParam('error_handler');
		switch ($errors->type) {
			case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER :
			case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION :
				$message = sprintf($this->translate->_('ERRORS_HTTP_STATUS_CODE_404'), $this->view->url());
				$this->log->err($message);
				break;
			default :
				$e = $errors['exception'];
				switch ($e->getCode()) {
					case 301 : // Moved Permanently
						$message = sprintf($this->translate->_('ERRORS_HTTP_STATUS_CODE_301'), $e->getMessage());
						$this->log->err($message);
						break;
					case 401 : // Unauthroized
						$message = sprintf($this->translate->_('ERRORS_HTTP_STATUS_CODE_401'), $this->view->url());
						$this->log->err($message);
						$this->logAuth();
						break;						
					case 403 : // Forbidden
						$message = $this->translate->_('ERRORS_HTTP_STATUS_CODE_500');
						$entry = sprintf($this->translate->_('ERRORS_HTTP_STATUS_CODE_403'), $this->view->url());
						$this->log->err($entry);
						$this->logAuth();
						break;
					case 404 : // Not Found
						$message = sprintf($this->translate->_('ERRORS_HTTP_STATUS_CODE_404'), $this->view->url());
						$this->log->err($message);
						break;
					case 466 : // Blacklisted IP
						$message = $this->translate->_('ERRORS_HTTP_STATUS_CODE_500');
						$entry = sprintf($this->translate->_('ERRORS_HTTP_STATUS_CODE_466'), $e->getMessage());
						$this->log->err($entry);
						break;
					default :
						$message = $this->translate->_('ERRORS_HTTP_STATUS_CODE_500');						
						$this->log->err($message);
						$e = $errors['exception'];
						$trace = $e->getTrace();
						$entry = sprintf($this->translate->_('LOG_ERROR_EXCEPTION_TRACE'), $trace[0]['class'], $trace[0]['function']);
						$this->log->err($entry);
						$entry = sprintf($this->translate->_('LOG_ERROR_EXCEPTION_CODE'), $e->getCode());
						$this->log->err($entry);
						$entry = sprintf($this->translate->_('LOG_ERROR_EXCEPTION_MESSAGE'), $e->getMessage());
						$this->log->err($entry);
						$this->logAuth();
						break;
				}
				break;
		}

		$this->view->message = $message;
	}
	
	protected function logAuth() {
		$auth = Zend_Registry::get('WJAuth');
		if ($auth->isActive()) {
			$user = $auth->authRecord();
			$format = $this->translate->_('LOG_ERROR_LOGAUTH_USER');
			$entry = sprintf($format, $user->user_id, $user->user_username, $user->user_email, $_SERVER['REMOTE_ADDR']);
			$this->log->err($entry);
		} else {
			$format = $this->translate->_('LOG_ERROR_LOGAUTH_NOUSER');
			$entry = sprintf($format, $_SERVER['REMOTE_ADDR']);
			$this->log->err($entry);
		}
	}
}

?>